//
//  SettingViewController.m
//  Civacrm
//
//  Created by zhk on 2017/2/7.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "SettingViewController.h"
#import "FeedbackViewController.h"
#import "MessageViewController.h"
#import "HelpCenterViewController.h"
#import "AboutUsViewController.h"

static NSString * MySettingTableViewCellReuseID = @"SettingCell";

@interface SettingViewController ()

@property (nonatomic , strong) UITableView * settingTableView;

@property (nonatomic , strong) NSMutableArray * dataArray;

@property (nonatomic , strong) UILabel * lineLabel;

@end

@implementation SettingViewController

-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithObjects:@"消息",@"清理缓存",@"关于我们",@"帮助中心",@"意见反馈", nil];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigation];
    [self initView];
}

- (void)initNavigation
{
    self.title = @"设置";
}

- (void)initView
{
    self.settingTableView = [[UITableView alloc]initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    [self.view addSubview:self.settingTableView];
    self.settingTableView.delegate = self;
    self.settingTableView.dataSource = self;
    [self.settingTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MySettingTableViewCellReuseID];
    self.settingTableView.backgroundColor = GlobalBgColor;
    self.settingTableView.rowHeight = 50;
    self.settingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIButton * loginOutBtn = [[UIButton alloc]init];
    [loginOutBtn setBackgroundColor:[UIColor whiteColor]];
    [loginOutBtn setTitle:@"注销当前账号" forState:(UIControlStateNormal)];
    loginOutBtn.frame = CGRectMake(0, 62, kScreenWidth, 50);
    [loginOutBtn setTitleColor:ColorWithHexString(@"f36d6d") forState:(UIControlStateNormal)];
    [loginOutBtn addTarget:self action:@selector(handleLoginOutButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 100)];
    bgView.userInteractionEnabled = YES;
    bgView.backgroundColor = [UIColor clearColor];
    [bgView addSubview:loginOutBtn];
    self.settingTableView.tableFooterView.userInteractionEnabled = YES;
    self.settingTableView.tableFooterView = bgView;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
            // 消息
            [self toMessage];
        }
            break;
        case 1:
        {
            // 清理缓存
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"Civa将清理手机里的缓存文件，是否让Civa清理？" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction * done = [UIAlertAction actionWithTitle:@"我再想想" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            }];
            [alert addAction:done];
            UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"去清理吧" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [[SDImageCache sharedImageCache] clearMemory];
                [[SDImageCache sharedImageCache] clearDisk];
                [self showSuccessWithStatus:@"清理完成"];

            }];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:nil];
        }
            break;
        case 2:
        {
            // 关于我们
            [self toAboutUs];
        }
            break;
        case 3:
        {
            // 帮助中心
            [self toHelpCenter];
        }
            break;
        case 4:
        {
            // 意见反馈
            [self toFeedback];
        }
            break;
            
        default:
            break;
    }
}

- (void)toFeedback
{
    FeedbackViewController * feedbackVC = [[FeedbackViewController alloc]init];
    [self.navigationController pushViewController:feedbackVC animated:YES];
}

- (void)toMessage
{
    MessageViewController * messageVC = [[MessageViewController alloc]init];
    [self.navigationController pushViewController:messageVC animated:YES];
}

- (void)toAboutUs
{
    AboutUsViewController * aboutUsVC = [[AboutUsViewController alloc]init];
    [self.navigationController pushViewController:aboutUsVC animated:YES];
}

- (void)toHelpCenter
{
    HelpCenterViewController * helpCenter = [[HelpCenterViewController alloc]init];
    [self.navigationController pushViewController:helpCenter animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MySettingTableViewCellReuseID forIndexPath:indexPath];
    cell.textLabel.font = font(32);
    cell.textLabel.textColor = ColorWithHexString(@"4e778f");
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    self.lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 49, kScreenWidth - 20, 1)];
    self.lineLabel.backgroundColor = ColorWithHexString(@"f2f9fa");
    [cell.contentView addSubview:self.lineLabel];
    
    return cell;
}


- (void)handleLoginOutButtonAction:(UIButton *)sender
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"注销账号" message:@"\n您要留下Civa孤零零一个人吗？\n" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction * done = [UIAlertAction actionWithTitle:@"真的要离开" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
       [self initNetWork];
    }];
    [alert addAction:done];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"继续陪Civa" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
    
}


#pragma mark - networkAndNetworkDelegate -
- (void)initNetWork{
    
    self.params[@"userId"] = self.userID;
    [self.manager postWithURL:NET_LOGOUT param:self.params delegate:self isEncrypt:YES type:NetWorkingType0];
    
}

- (void)networkingHandleSuccess:(NetworkingHandle *)handle parameters:(NSDictionary *)parameters responseObject:(id)responseObject type:(NetWorkingType)type{
    switch (type) {
        case NetWorkingType0:
        {
            [self removeUserLoginInfo];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}

- (void)networkingHandlefail:(NetworkingHandle *)handle type:(NetWorkingType)type{
}

- (void)networkingHandleWarning:(NetworkingHandle *)handle responseObject:(id)responseObject type:(NetWorkingType)type{
    
}




@end
